package cn.bigcontainer.alt.meta.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.common.util.BigConstant;
import cn.bigcontainer.alt.meta.dto.MAppDto;
import cn.bigcontainer.alt.meta.dto.MCrud;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.po.MApp;
import cn.bigcontainer.alt.meta.po.MViewProperty;
import cn.bigcontainer.alt.meta.service.MCrudService;

@Controller
@RequestMapping("/meta/" + MAppDto.TYPE_CRUD)
@SessionAttributes(BigConstant.SESSION_USER)
public class MCrudController {

	@Autowired
	private MCrudService crudService;

	/**
	 * 
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadList")
	public String loadListData(MCrud app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MViewDto list = crudService.loadListData(app.getId(), req);
		map.put("list", list);
		MCrud crud = (MCrud) list.getApp();
		crud.setDomprefix(app.getDomprefix());
		crud.setSelector(app.getSelector());
		return list.getApp().getListTemplate();
	}

	/**
	 * 加载创建页面
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadCreateForm")
	public String loadCreateForm(MAppDto app, String selectedIds, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MViewDto form = crudService.loadCreateForm (app.getId(), selectedIds, req);
		form.getApp().setDomprefix(app.getDomprefix());
		map.put("form", form);
		return "meta/form";
	}

    @PostMapping("/create")
    @ResponseBody
    public BigResultCode create(MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        MViewDto form = crudService.submitCreateForm(app.getId(), req);
        form.getApp().setDomprefix(app.getDomprefix());
        return BigResultCode.getSuccessCode();
    }

	/**
	 * 加载修改表单
	 * @param app
	 * @param selectedIds
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadUpdateForm")
	public String loadUpdateForm(MAppDto app, String selectedIds, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MViewDto form = crudService.loadUpdateForm (app.getId(), selectedIds, req);
		form.getApp().setDomprefix(app.getDomprefix());
		map.put("form", form);
		return "meta/form";
	}

	/**
	 * 提交修改表单
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
    @RequestMapping("/update")
	@ResponseBody
    public BigResultCode update (MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        MViewDto vw = crudService.submitUpdateForm(app.getId(), req);
        return BigResultCode.getSuccessCode().setContent(vw.getCallBackData());
    }

	/**
	 *
	 * @param app
	 * @param selectedIds
	 * @param req
	 * @param map
	 * @return
	 */
    @RequestMapping("/delete")
	@ResponseBody
    public BigResultCode deleteInstances (MAppDto app, String selectedIds, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        crudService.deleteObjects(app.getId(), selectedIds, req);
        return BigResultCode.getSuccessCode();
    }

	@RequestMapping("")
	public String loadCrud(MCrud app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MCrud crud = crudService.loadCrud(app.getId(), req);
		crud.setDomprefix(app.getDomprefix());
		crud.setSelector(app.getSelector());
		crud.setSelectorsingle(app.getSelectorsingle());
		map.put("app", crud);
		return "meta/" + MAppDto.TYPE_CRUD;
	}

	/**
	 * 获取视图属性propertyId关联的crud的name值
	 * @param app
	 * @param propertyId
	 * @param propertyValue
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadSelectorNameValues")
    @ResponseBody
	public BigResultCode loadSelectorNameValues(MApp app, String propertyId, String propertyValue, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MViewProperty prop = crudService.loadSelectorNameValues(app.getId(), propertyId, propertyValue, req);
		return BigResultCode.getSuccessCode().setContent(prop.getData().get(MViewProperty.PARAM_NAME_VALUE));
	}

	/**
	 * 根据主键值，获取名称字段的值
	 * @param id appId
	 * @param propertyValue 主键值
	 * @return
	 */
	@RequestMapping("/loadSelectorNames")
	@ResponseBody
	public BigResultCode loadSelectorNameValues(String id, String propertyValue) {
		Object[] nameValues = crudService.loadSelectorNameValues(id, propertyValue);
		return BigResultCode.getSuccessCode().setContent(nameValues);
	}

	@RequestMapping("/import")
	@ResponseBody
	public BigResultCode importExcel (MAppDto app, Integer fileId, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		crudService.importExcel(app.getId(), fileId, req);
		return BigResultCode.getSuccessCode();
	}

}
